package pl.vanta.utils.gui.sliders;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.BoundedRangeModel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicSliderUI;

public class VSlider extends JSlider implements MouseListener, MouseWheelListener
{
	private static final long	serialVersionUID	= 1L;

	private int					blockSize;

	public VSlider()
	{
		super();

		init();
	}

	public VSlider(BoundedRangeModel brm)
	{
		super(brm);

		init();
	}

	public VSlider(int orientation, int min, int max, int value)
	{
		super(orientation, min, max, value);

		init();
	}

	public VSlider(int min, int max, int value)
	{
		super(min, max, value);

		init();
	}

	public VSlider(int min, int max)
	{
		super(min, max);

		init();
	}

	public VSlider(int orientation)
	{
		super(orientation);

		init();
	}

	public int getBlockSize()
	{
		return this.blockSize;
	}

	public void setBlockSize(int blockSize)
	{
		if (blockSize >= 0)
		{
			this.blockSize = blockSize;
		}
	}

	private void init()
	{
		this.addMouseListener(this);
		this.addMouseWheelListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{

	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		
	}

	@Override
	public void mouseExited(MouseEvent e)
	{

	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		if(this.isEnabled())
		{
			int newValue = getValue();

			BasicSliderUI sliderUI = (BasicSliderUI) getUI();

			if (this.orientation == SwingConstants.VERTICAL)
			{
				newValue = sliderUI.valueForYPosition((int) e.getPoint().getY());
			}
			else if (this.orientation == SwingConstants.HORIZONTAL)
			{
				newValue = sliderUI.valueForXPosition((int) e.getPoint().getX());
			}

			setValue(newValue);
			
			e.consume();
		}
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e)
	{
		if (isEnabled())
		{
			int rot = e.getWheelRotation();
			int step = getMinorTickSpacing();

			setValue(getValue() + rot * step);
		}
	}
}